#include "Game.h"
#include "OgreStringConverter.h"
CGame* CGame::gGame;
CGame& CGame::getSingleton() {
    if (!gGame) {
        gGame = new CGame();
    }
    return *gGame;
}
CGame* CGame::getSingletonPtr() {
    if (!gGame) {
        gGame = new CGame();
    }
    return gGame;
}

CGame::CGame() {
	m_state = STARTUP;
}

CGame::~CGame() {
}


GameState CGame::getCurrentState() {
	return m_state;
}

// for the sake of clarity, I am not using actual thread synchronization 
// objects to serialize access to this resource. You would want to protect
// this block with a mutex or critical section, etc.
bool CGame::lockState() {
	if (m_locked == false) {

		m_locked = true;
		return true;
	}
	else
		return false;
}

bool CGame::unlockState() {
	if (m_locked == true) {
		m_locked = false;
		return true;
	}
	else
		return false;
}

bool CGame::requestStateChange(GameState newState) {
	if (m_state == STARTUP) {
		m_locked = false;
		m_state = newState;

		return true;
	}

	// this state cannot be changed once initiated
	if (m_state == SHUTDOWN) {
		return false;
	}

	if ((m_state == GUI || m_state == SIMULATION || m_state == LOADING || m_state == CANCEL_LOADING) && 
			(newState != STARTUP) && (newState != m_state)) {
		m_state = newState;
		return true;
	}
	else
		return false;
}

void CGame::setFrameTime(float ms) {
	m_frame_time = ms;
}


